package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.exception.ApiException;
import com.ytjj.qmyx.admin.mapper.ChannelUsersMapper;
import com.ytjj.qmyx.admin.model.ChannelUsers;
import com.ytjj.qmyx.admin.model.request.ChannelUsersRequest;
import com.ytjj.qmyx.admin.model.response.ChannelUsersResponse;
import com.ytjj.qmyx.admin.service.ChannelUsersService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ChannelUsersServiceImpl implements ChannelUsersService {

    @Resource
    private ChannelUsersMapper channelUsersMapper;

    @Value("${shiroConfigure.salt}")
    private String salt;

    @Value("${shiroConfigure.hashIterations}")
    private int hashIterations;

    @Override
    public Integer add(ChannelUsersRequest request) {
        //查询手机号码是否存在
        if (channelUsersMapper.checkTel(request.getTel()) != null){
            throw new ApiException("该用户已存在");
        }
        Md5Hash md5SaltIterateHash = new Md5Hash(request.getPassword(), salt, hashIterations);
        request.setPassword(md5SaltIterateHash.toString());
        request.setCreateTime(new Date());
        return channelUsersMapper.insert(request);
    }

    @Override
    public Integer delete(Integer id) {
        return channelUsersMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer update(ChannelUsersRequest request) {
        ChannelUsers users = channelUsersMapper.selectByPrimaryKey(request.getId());
        if (users.getPassword().equals(request.getPassword())) {
            request.setPassword(null);
        } else {
            if (StringUtils.isBlank(request.getPassword())) {
                request.setPassword(null);
            } else {
                Md5Hash md5SaltIterateHash = new Md5Hash(request.getPassword(), salt, hashIterations);
                request.setPassword(md5SaltIterateHash.toString());
            }
        }
        //查询手机号码是否存在
        ChannelUsersResponse check = channelUsersMapper.checkTel(request.getTel());
        if (check != null && request.getId().compareTo(check.getId()) != 0){
            throw new ApiException("该用户已存在");
        }
        request.setUpdateTime(new Date());
        return channelUsersMapper.updateByPrimaryKeySelective(request);
    }

    @Override
    public CommonPage<ChannelUsersResponse> select(ChannelUsersRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<ChannelUsersResponse> list = channelUsersMapper.select(request);
        return CommonPage.restPage(list);
    }

    /**
     * 查询type=5的正常渠道用户
     * @return
     */
    @Override
    public List<ChannelUsersResponse> channelUsersByType() {
        List<ChannelUsersResponse> list = channelUsersMapper.channelUsersByType();
        if (CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        list.forEach(item ->{
            String s = item.getId() + "-" + item.getTel();
            item.setShowChannelUsers(s);
        });
        return list;
    }
}
